using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using SpaceShooter.Components;
using SpaceShooter.Extensions;
using SpaceShooter.ParticleSystem;
using SpaceShooter.Screens;

namespace SpaceShooter
{
    public enum DisplayMode
    {
        FullScreen,
        Windowed
    }

    public enum ScreenResolution
    {
        Native,
        Res640_480,
        Res800_600,
        Res1024_768,
        Res1280_1024,
        Res1600_1200,
        Res1440_900,
        Res1680_1050,
        Res1920_1080,
        Res2560_1600
    }

    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class BaseGame : Microsoft.Xna.Framework.Game
    {
        private static object locker = new object();
        private static BaseGame game;

        GraphicsDeviceManager graphics;

        private Stack<IGameScreen> screens = new Stack<IGameScreen>();

        public BaseGame()
        {
            lock (locker)
            {
                if (game != null)
                    throw new Exception("The Game was already initialized!");
                game = this;
            }

            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            Components.Add(new MissileManagerComponent(this));

            this.SetScreenResolution(DisplayMode.Windowed, ScreenResolution.Res1024_768);
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            this.AddScreen(new StartScreen(this));

            ParticleSystemManager.RegisterParticleSystems(this);

            base.Initialize();
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (this.screens.Count > 0)
            {
                this.screens.Peek().Update(gameTime);
            }

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            if (this.screens.Count > 0)
            {
                this.screens.Peek().Render(gameTime);
            }

            base.Draw(gameTime);
        }

        public void AddScreen(IGameScreen screen)
        {
            screen.Initialize();
            screen.LoadContent();
            game.screens.Push(screen);
        }

        public void ExitScreen()
        {
            IGameScreen screen = game.screens.Pop();
            screen.UnloadContent();

            if (game.screens.Count == 0)
                game.Exit();
        }

        public IGameScreen CurrentScreen
        {
            get { return game.screens.Peek(); }
        }

        public void SetScreenResolution(DisplayMode displayMode, ScreenResolution resolution)
        {
            bool isFullscreen = displayMode == DisplayMode.FullScreen;

            int resX, resY;
            this.GetScreenResolutionFromEnum(resolution, out resX, out resY);

            this.graphics.PreferredBackBufferWidth = resX;
            this.graphics.PreferredBackBufferHeight = resY;
            this.graphics.IsFullScreen = isFullscreen;
            this.graphics.ApplyChanges();
        }

        private void GetScreenResolutionFromEnum(ScreenResolution resolution, out int resX, out int resY)
        {
            if (resolution == ScreenResolution.Native)
            {
                resY = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
                resX = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
            }
            else
            {
                string res = resolution.ToString();
                res = res.Substring(3);

                var parts = res.Split('_');

                resX = int.Parse(parts[0]);
                resY = int.Parse(parts[1]);
            }
        }
    }
}
